package com.duke.shaking.widget.gallery;

import java.util.List;

import com.androidquery.AQuery;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

public class GalleryAdapter extends BaseAdapter {

	private AQuery aQuery;
	private int ownposition;
	private List<String> imagesUrl;
	private List<String> thumbnailsUrl;
	private Context context;
	
	public GalleryAdapter(Context context, 
			List<String> imagesUrl, List<String> thumbnailsUrl) {
		this.context = context;
		this.imagesUrl = imagesUrl;
		this.thumbnailsUrl = thumbnailsUrl;
		
		aQuery = new AQuery(context);
	}
	
	public int getOwnposition() {
		return ownposition;
	}

	public void setOwnposition(int ownposition) {
		this.ownposition = ownposition;
	}

	public int getCount() {
		if (imagesUrl == null) {
			return 0;
		}
		return imagesUrl.size();
	}

	public Object getItem(int position) { 
		ownposition = position;
		return position;
	}

	public long getItemId(int position) {
		ownposition = position; 
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ownposition = position;
		ViewHolder viewHolder;
		if (convertView == null) {

			RelativeLayout layout = new RelativeLayout(context);
			GalleryExt.LayoutParams lpGallery = new GalleryExt.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			layout.setLayoutParams(lpGallery);
			
			RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			lpImg.addRule(RelativeLayout.CENTER_IN_PARENT);
			
			ImageView imageview = new ImageView(context);
			imageview.setBackgroundColor(Color.BLACK);
			imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imageview.setLayoutParams(lpImg);
			layout.addView(imageview);
			
			RelativeLayout.LayoutParams lpPro = new RelativeLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			lpPro.addRule(RelativeLayout.CENTER_IN_PARENT);
			
			ProgressBar progressBar = new ProgressBar(context);
			progressBar.setLayoutParams(lpPro);
			layout.addView(progressBar);
			//progressBar.setVisibility(View.GONE);
			
			viewHolder = new ViewHolder();
			viewHolder.imageView = imageview;
			viewHolder.progressBar = progressBar;
			layout.setTag(viewHolder);
			convertView = layout;
		}
		else {
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		/*
		if (thumbnailsUrl != null && position < thumbnailsUrl.size() && viewHolder != null) {
			Bitmap thumbnail = aQuery.getCachedImage(thumbnailsUrl.get(position));
			if (thumbnail != null) {
				Log.i(this.getClass().getName(), "set thumbnail: " + thumbnailsUrl.get(position));
				aQuery.id(viewHolder.imageView).image(thumbnail);
			}
		}
		*/
		if (imagesUrl != null && position < imagesUrl.size() && viewHolder != null) {
			Bitmap preset = null;
			if (thumbnailsUrl != null && position < thumbnailsUrl.size()) {
				if (thumbnailsUrl.get(position) != null) {
					preset = aQuery.getCachedImage(thumbnailsUrl.get(position));
				}
			}
			aQuery.id(viewHolder.imageView).progress(viewHolder.progressBar).image(
					imagesUrl.get(position), false, true, 0, 0, preset, AQuery.FADE_IN);
		}
		//imageview.setImageResource(images.get(position));
		// imageview.setAdjustViewBounds(true);
		// imageview.setLayoutParams(new GridView.LayoutParams(320, 480));
		// imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		return convertView;
	}
	
	private class ViewHolder {
		public ImageView imageView;
		public ProgressBar progressBar;
	}

}
